package leetcode;

public class Q0134 {
    public int canCompleteCircuit(int[] gas, int[] cost) {
        int sum = 0;
        int minSum = 0x3f3f3f3f;
        int res = -1;
        for (int i = 0; i < gas.length; i++) {
            gas[i] -= cost[i];
            sum += gas[i];
            if (sum <= minSum) {
                minSum = sum;
                res = i;
            }
        }
        if (sum < 0) return -1;
        return (res + 1) % gas.length;
    }
}
